// page/details/pages/referralCode/referralCode.js
const app = getApp()
let login = require('../../../../config/login');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      mobile: '',
      referral_code: '',
      phone_sign: "",
      nickname: ''
    },


  },
  // 获取用户名
  getName(e) {
    const that = this;
    that.setData({
      [`formData.nickname`]: e.detail.value
    })
  },

  // 获取手机号
  getPhoneNumber(e) {
    const that = this;
    console.log(e, 9999)
    if (e.detail.errMsg == "getPhoneNumber:ok") {

      let json = {
        code: e.detail.code
      }
      let jsonData = app.utils.getSignData(json);
      that.setData({
        hiddenLoading: false
      })
      app.utils.post(app.api.reqUrls.getMiniPhoneNoBind, jsonData).then(res => {
        console.log(res)
        that.setData({
          hiddenLoading: true
        })
        if (res.status == 1) {
          that.setData({
            'formData.mobile': res.phoneinfo.mobile,
            'formData.phone_sign': res.phoneinfo.phone_sign
          })

        } else {
          throw res.msg
        }
      }).catch(err => {
        that.setData({
          hiddenLoading: true
        })
        app.showInfo(err.toString())
      })

    } else {
      return false;
    }

  },
  parseQueryString(query) {
    let scene = decodeURIComponent(query)
    let url = scene.substring(scene.indexOf('?') + 1)
    const param = {};
    const pairs = url.split('&');
    pairs.forEach(function (pair) {
      pair = pair.split('=');
      param[pair[0]] = decodeURIComponent(pair[1] || '');
    });
    return param;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    let sceneObj = that.parseQueryString(options.q)
    login.checkLogin().then(res => {
      const userInfo = wx.getStorageSync('userInfo')
      that.setData({
        'formData.nickname': userInfo.nickname,
        'formData.referral_code': sceneObj.code,
        'formData.mobile': userInfo.mobile,
      })
    }).catch(err => {
      app.appCallBack = (e) => {
        const userInfo = wx.getStorageSync('userInfo')
        that.setData({
          'formData.nickname': userInfo.nickname,
          'formData.referral_code': sceneObj.code,
          'formData.mobile': userInfo.mobile,
        })

      }
    })
  },

  Apply() {
    let that = this;
    wx.showModal({
      content: '是否提交申请成为分销商?',
      success(prop) {
        if (prop.confirm) {
          if (!that.data.formData.mobile) {
            app.showInfo("请选择手机号码")
            return;
          }
          if (!that.data.formData.nickname) {
            app.showInfo("请填写用户名称")
            return;
          }
          let json = {
            referral_code: that.data.formData.referral_code,
            token: app.globalData.token,
            mobile: that.data.formData.mobile,
            phone_sign: that.data.formData.phone_sign,
            nickname: that.data.formData.nickname,
          }
          let jsonData = app.utils.getSignData(json); // 添加签名
          wx.showLoading({
            title: '加载中...',
          })
       
          app.utils.post(app.api.reqUrls.registerProxy, jsonData).then(res => {
            wx.hideLoading();
            console.log(res, 89898989)
            if (res.status == 1) {
              wx.showModal({
                content: res.msg,
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#ff5151',
                success(e) {
                  wx.switchTab({
                    url: '/page/tabBar/index/index'
                  })
                }
              })
            } else {
              app.showInfo(res.msg)
            }
          })
          console.log('用户点击确定')
        } else if (prop.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})